package com.sad.jetpack.v1.datamodel.api.extension.client.socket.ipc;

import org.json.JSONObject;

import java.nio.charset.StandardCharsets;

public class ErrorMessage {
    private int code=0;
    private String error="";

    public int getCode() {
        return code;
    }

    public void setCode(int code) {
        this.code = code;
    }

    public String getError() {
        return error;
    }

    public void setError(String error) {
        this.error = error;
    }

    public ErrorMessage(int code, String error) {
        this.code = code;
        this.error = error;
    }

    public byte[] toBytes(){
        try {
            JSONObject j=new JSONObject();
            j.put("error_code",getCode());
            j.put("error_msg",getError());
            String s=j.toString();
            return s.getBytes(StandardCharsets.UTF_8);
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }

    public static ErrorMessage parse(byte[] bytes){
        try {
            JSONObject j=new JSONObject(new String(bytes,StandardCharsets.UTF_8));
            ErrorMessage errorMessage=new ErrorMessage(j.optInt("error_code"),j.optString("error_msg"));
            return errorMessage;
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }
}
